package com.winning.sx.microframework.service.sysmanage;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.dao.sysmanage.UserManageDao;
import com.winning.sx.microframework.model.SelectModel;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.manage.UserManageModel;
import com.winning.sx.microframework.common.util.BaseUtil;
import com.winning.sx.microframework.common.util.EncryptionUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoY on 2017/2/13.
 */
@Service
public class UserManageService {
    @Autowired
    private UserManageDao userManageDao;

    public DataTable queryUsers() throws CustomException {
        return userManageDao.queryUserInfo();
    }

    public enum opType {
        insert, update, resetpassword, updatepassword, delete;

        public static opType toOPType(String str) {
            try {
                return valueOf(str);
            } catch (Exception ex) {
                return insert;
            }
        }
    }

    public int editUser(String type, UserManageModel userModel) throws Exception {
        int result = -1;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userModel.setcInputTime(sdf.format(date));

        Map userMap = BaseUtil.beanToMap(userModel);
        switch (opType.toOPType(type)) {
            case insert:
                userModel.setcUserID(BaseUtil.createUUID());
                userModel.setcPassword(EncryptionUtil.getHash(userModel.getcPassword()));
                userMap = BaseUtil.beanToMap(userModel);
                result = userManageDao.newUserInfo(userMap);
                break;
            case update:
                result = userManageDao.editUserInfo(userMap);
                break;
            case resetpassword:
                userModel.setcPassword(EncryptionUtil.getHash("123456"));
                userMap = BaseUtil.beanToMap(userModel);
                result = userManageDao.updatePassword(userMap);
                break;
            case updatepassword:
                userModel.setcUserID(((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcUserID());
                userModel.setcUserCode(((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcUserCode());
                userModel.setcPassword(EncryptionUtil.getHash(userModel.getcPassword()));
                userMap = BaseUtil.beanToMap(userModel);
                result = userManageDao.updatePassword(userMap);
                break;
            case delete:
                result = userManageDao.deleteUserInfo(userMap);
                break;
        }
        return result;
    }

    /*public List<SelectModel> getDeptList() {
        return userManageDao.getDeptList();
    }*/

    public DataTable queryURasso(Map map) throws CustomException {
        return userManageDao.queryURasso(map);
    }

    public int saveAsso(Map map) throws CustomException {
        return userManageDao.saveAsso(map);
    }

    //public int syncDept() throws CustomException {
    //    return userManageDao.syncDept();
    //}

}
